package com.stone.design.practice.customview.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * @author Stone
 * @version V1.0
 * @description:
 * @date 4/14/18 11:08
 * @email redsstone@163.com
 */
public class PieView extends View{

    // 颜色值
    private int[] mColors = {0xFFCCFF00, 0xFF6495ED, 0xFFE32636, 0xFF800000, 0xFF808000, 0xFFFF8C69, 0xFF808080,
            0xFFE6B800, 0xFF7CFC00};

    Paint mPaint = new Paint();

    // 饼状图初始绘制角度
    private float mStartAngle = 0;

    private int mWidth, mHeight;
    private List<PieData> mData;

    public PieView(Context context) {
        this(context, null);
    }

    public PieView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PieView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (mData == null) {
            return;
        }

        float currentStartAngle = mStartAngle;
        canvas.translate(mWidth/2, mHeight/2);
        float r = (float) (Math.min(mHeight, mWidth)/2 * 0.8);
        RectF rectF = new RectF(-r, -r, r, r);

        for (int i = 0; i < mData.size(); i++) {
            PieData pieData = mData.get(i);
            mPaint.setColor(pieData.getColor());
            canvas.drawArc(rectF, currentStartAngle, pieData.getAngle(), true, mPaint);
            currentStartAngle += pieData.getAngle();
        }
    }

    public void setStartAngle(int startAngle) {
        mStartAngle = startAngle;
        invalidate();
    }

    public void setData(List<PieData> mData) {
        this.mData = mData;
        initData(mData);
        invalidate(); // 刷新
    }
    /**
     * 初始化画笔
     */
    private void initPaint() {
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(2f);
        mPaint.setAntiAlias(true);
    }

    private void initData(List<PieData> mData) {
        if (mData == null || mData.size() == 0) {
            return;
        }

        float sumValue = 0;
        for (int i = 0; i < mData.size(); i++) {
            PieData pieData = mData.get(i);
            sumValue += pieData.getValue();
            int j = i % mColors.length;

            pieData.setColor(mColors[j]);
        }

        for (int i = 0; i < mData.size(); i++) {
            PieData pieData = mData.get(i);

            float percentage = pieData.getValue() / sumValue;
            float angle = percentage * 360;

            pieData.setPercentage(percentage);
            pieData.setAngle(angle);

        }
    }
}
